/**
 *
 */
package runtime;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import javax.sql.DataSource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * @author downpour
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:/sms/applicationContext-*.xml", "classpath*:/sms/dev/applicationContext-*.xml" })
public class DatabaseSetting {

    private static final Logger logger = LoggerFactory.getLogger(DatabaseSetting.class);

    @Autowired
    private DataSource dataSource;

    /**
     * Set up database schema, expect root username and password
     * 
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        logger.info("Setting up Schema.");
        
        // using project relative folder;
        Resource script = new ClassPathResource("schema/schema.sql");

        ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
        populator.addScript(script);
        populator.setSqlScriptEncoding("UTF-8");

        String url = "jdbc:mysql://127.0.0.1:3306/";

        // read username from inputstream
        logger.info("Your database connection user name: ");

        InputStreamReader inputStreamReader = new InputStreamReader(System.in);
        String name = new BufferedReader(inputStreamReader).readLine();

        // read password from inputstream
        logger.info("Your database connection password: ");

        inputStreamReader = new InputStreamReader(System.in);
        String password = new BufferedReader(inputStreamReader).readLine();

        DriverManagerDataSource dataSource = new DriverManagerDataSource(url, name, password);
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        
        DatabasePopulatorUtils.execute(populator, dataSource);

    }

    /**
     * Update database table and import init_data.sql from etc
     */
    @Test
    public void initData() {

        logger.info("Setting up init data.");

        Resource script = new ClassPathResource("scheme/init_data.sql");

        ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
        populator.addScript(script);
        populator.setSqlScriptEncoding("UTF-8");

        DatabasePopulatorUtils.execute(populator, dataSource);

    }
    
    /**
     * Update database table and import mock_data.sql from classpath
     */
    @Test
    public void mockData() {

        logger.info("Setting up mock data.");

        Resource script = new ClassPathResource("scheme/mock_data.sql");

        ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
        populator.addScript(script);
        populator.setSqlScriptEncoding("UTF-8");

        DatabasePopulatorUtils.execute(populator, dataSource);

    }

    /**
     * Update database table
     */
    @Test
    public void updateSchema() {
        logger.info("Setting up mock data.");

        Resource script = new ClassPathResource("scheme/table_test.sql");

        ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
        populator.addScript(script);
        populator.setSqlScriptEncoding("UTF-8");

        DatabasePopulatorUtils.execute(populator, dataSource);
    }

}